﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Assist;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.MEP.Electric.Common;

namespace HYRElectric.PowerSystem
{
	public partial class SystemGraphSetDlg : System.Windows.Forms.Form
	{
		public SystemGraphSetDlg()
		{
			this.InitializeComponent();
			base.Paint += this.CSystemGraphSetDlg_Paint;
			this.cbxnVector.Items.Add("横向");
			this.cbxnVector.Items.Add("竖向");
			for (int i = 1; i < 17; i++)
			{
				this.cbxBusWidth.Items.Add(i);
				this.cbxWireWidth.Items.Add(i);
			}
			this.LoadXMLData();
		}

		private void CSystemGraphSetDlg_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
		}

		private void CopyFile(string sourceFileName, string destFileName)
		{
			try
			{
				File.Copy(sourceFileName, destFileName);
			}
			catch (Exception)
			{
			}
		}

		private void LoadXMLData()
		{
			if (!File.Exists(this.xmlFile))
			{
				string text = "SystemGraphSet.xml";
				string sourceFileName = AssistFunc.GetAppRootPath() + "\\Data\\ElectricalData\\" + text;
				string destFileName = Product.WorkingLocation + text;
				this.CopyFile(sourceFileName, destFileName);
			}
			this.xmlHelper.LoadXml(this.xmlFile, "Boot");
			this.xmlHelper.SelectNode(Path.Combine("Boot", "HYSystemGrapSet", "InLine"));
			this.txtnInline.Text = this.xmlHelper.GetAttribute("InLine");
			this.txtnlnLink.Text = this.xmlHelper.GetAttribute("InLink");
			this.xmlHelper.SelectNode(Path.Combine("Boot", "HYSystemGrapSet", "SystemLoop"));
			this.txtnOutLine.Text = this.xmlHelper.GetAttribute("OutLine");
			this.txtnOutLink.Text = this.xmlHelper.GetAttribute("OutLink");
			this.txtnLoopSpace.Text = this.xmlHelper.GetAttribute("LoopSpace");
			this.xmlHelper.SelectNode(Path.Combine("Boot", "HYSystemGrapSet", "Other"));
			int num = Convert.ToInt32(this.xmlHelper.GetAttribute("BusWidth"));
			this.cbxBusWidth.SelectedItem = num;
			int num2 = Convert.ToInt32(this.xmlHelper.GetAttribute("WireWidth"));
			this.cbxWireWidth.SelectedItem = num2;
			this.txtdTextHeight.Text = this.xmlHelper.GetAttribute("TextHeight");
			int selectedIndex = Convert.ToInt32(this.xmlHelper.GetAttribute("Vector"));
			this.cbxnVector.SelectedIndex = selectedIndex;
		}

		private void SaveXMLData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Boot", "HYSystemGrapSet"));
			this.xmlHelper.SelectNode(Path.Combine("Boot", "HYSystemGrapSet", "InLine"));
			this.xmlHelper.SetAttribute("InLine", this.txtnInline.Text);
			this.xmlHelper.SetAttribute("InLink", this.txtnlnLink.Text);
			this.xmlHelper.SelectNode(Path.Combine("Boot", "HYSystemGrapSet", "SystemLoop"));
			this.xmlHelper.SetAttribute("OutLine", this.txtnOutLine.Text);
			this.xmlHelper.SetAttribute("OutLink", this.txtnOutLink.Text);
			this.xmlHelper.SetAttribute("LoopSpace", this.txtnLoopSpace.Text);
			this.xmlHelper.SelectNode(Path.Combine("Boot", "HYSystemGrapSet", "Other"));
			this.xmlHelper.SetAttribute("BusWidth", this.cbxBusWidth.SelectedItem.ToString());
			this.xmlHelper.SetAttribute("WireWidth", this.cbxWireWidth.SelectedItem.ToString());
			this.xmlHelper.SetAttribute("TextHeight", this.txtdTextHeight.Text);
			this.xmlHelper.SetAttribute("Vector", this.cbxnVector.SelectedIndex.ToString());
			this.xmlHelper.SaveXml("");
		}

		private void GetDefaultValue()
		{
			this.txtnInline.Text = "40";
			this.txtnlnLink.Text = "20";
			this.txtnOutLine.Text = "50";
			this.txtnOutLink.Text = "20";
			this.txtnLoopSpace.Text = "15";
			this.cbxBusWidth.SelectedItem = 6;
			this.cbxWireWidth.SelectedItem = 4;
			this.txtdTextHeight.Text = "3.5";
			this.cbxnVector.SelectedIndex = 0;
		}

		private void txtnInline_Enter(object sender, EventArgs e)
		{
			this.i = 1;
			this.g.DrawLine(new Pen(Color.Red, 2f), this.inLine, this.inLine_End);
		}

		private void txtnInline_Leave(object sender, EventArgs e)
		{
			this.g.DrawLine(new Pen(Color.Lime, 2f), this.inLine, this.inLine_End);
		}

		private void txtnlnLink_Enter(object sender, EventArgs e)
		{
			this.i = 2;
			this.g.DrawLine(new Pen(Color.Red, 2f), this.p_InLink_Start, this.p_InLink_End);
		}

		private void txtnlnLink_Leave(object sender, EventArgs e)
		{
			this.g.DrawLine(new Pen(Color.Lime, 2f), this.p_InLink_Start, this.p_InLink_End);
		}

		private void txtnOutLine_Leave(object sender, EventArgs e)
		{
			this.g.DrawLine(new Pen(Color.Lime, 2f), this.Feeder1_Start, this.Feeder1_End);
			this.g.DrawLine(new Pen(Color.Lime, 2f), this.Feeder2_Start, this.Feeder2_End);
		}

		private void txtnOutLine_Enter(object sender, EventArgs e)
		{
			this.i = 3;
			this.g.DrawLine(new Pen(Color.Red, 2f), this.Feeder1_Start, this.Feeder1_End);
			this.g.DrawLine(new Pen(Color.Red, 2f), this.Feeder2_Start, this.Feeder2_End);
		}

		private void txtnOutLink_Leave(object sender, EventArgs e)
		{
			this.g.DrawLine(new Pen(Color.Lime, 2f), this.inLink, this.inLink_End);
			this.g.DrawLine(new Pen(Color.Lime, 2f), this.inLink1, this.inLink1_End);
		}

		private void txtnOutLink_Enter(object sender, EventArgs e)
		{
			this.i = 4;
			this.g.DrawLine(new Pen(Color.Red, 2f), this.inLink, this.inLink_End);
			this.g.DrawLine(new Pen(Color.Red, 2f), this.inLink1, this.inLink1_End);
		}

		private void cbxBusWidth_Enter(object sender, EventArgs e)
		{
			this.i = 5;
			this.g.DrawLine(new Pen(Color.Red, 2f), this.bus_Width, this.bus_Width_End);
		}

		private void cbxBusWidth_Leave(object sender, EventArgs e)
		{
			this.g.DrawLine(new Pen(Color.Lime, 2f), this.bus_Width, this.bus_Width_End);
		}

		private void cbxWireWidth_Enter(object sender, EventArgs e)
		{
			this.i = 0;
		}

		private void ShowPanel()
		{
			this.g = this.panel2.CreateGraphics();
			this.blackPen = new Pen(Color.Lime, 2f);
			this.inLine = new System.Drawing.Point(23, 38);
			this.inLine_End = new System.Drawing.Point(68, 38);
			int num = 68;
			int num2 = 38;
			this.RepeatLine(num, num2);
			this.inLink = new System.Drawing.Point(num + 10 + 40, num2 - 20);
			this.inLink1 = new System.Drawing.Point(num + 10 + 40, num2 + 20);
			this.inLink_End = new System.Drawing.Point(num + 10 + 40 + 40, num2 - 20);
			this.inLink1_End = new System.Drawing.Point(num + 10 + 40 + 40, num2 + 20);
			this.bus_Width = new System.Drawing.Point(num + 10 + 40, num2 + 35);
			this.bus_Width_End = new System.Drawing.Point(num + 10 + 40, num2 - 35);
			this.g.DrawLine(this.blackPen, this.inLine, this.inLine_End);
			this.g.DrawLine(this.blackPen, this.inLink, this.inLink_End);
			this.g.DrawLine(this.blackPen, this.inLink1, this.inLink1_End);
			this.g.DrawLine(this.blackPen, this.bus_Width, this.bus_Width_End);
			this.RepeatLine(num + 10 + 40 + 40, num2 - 20);
			this.RepeatLine(num + 10 + 40 + 40, num2 + 20);
		}

		private void RepeatLine(int ii, int jj)
		{
			new System.Drawing.Point(ii, jj - 5);
			new System.Drawing.Point(ii, ii + 5);
			Point pt = new System.Drawing.Point(ii - 5, jj - 5);
			Point pt2 = new System.Drawing.Point(ii + 5, jj - 5);
			Point pt3 = new System.Drawing.Point(ii - 5, jj + 5);
			Point pt4 = new System.Drawing.Point(ii + 5, jj + 5);
			Point pt5 = new System.Drawing.Point(ii, jj + 10);
			Point point = new System.Drawing.Point(ii + 10, jj);
			Point point2 = new System.Drawing.Point(ii + 10 + 40, jj);
			this.g.DrawLine(this.blackPen, pt, pt4);
			this.g.DrawLine(this.blackPen, pt2, pt3);
			this.g.DrawLine(this.blackPen, pt5, point);
			if (this.count == 1)
			{
				this.p_InLink_Start = point;
				this.p_InLink_End = point2;
				this.g.DrawLine(this.blackPen, this.p_InLink_Start, this.p_InLink_End);
				this.count++;
				return;
			}
			if (this.count == 2)
			{
				this.Feeder1_Start = point;
				this.Feeder1_End = point2;
				this.g.DrawLine(this.blackPen, this.Feeder1_Start, this.Feeder1_End);
				this.count++;
				return;
			}
			if (this.count == 3)
			{
				this.Feeder2_Start = point;
				this.Feeder2_End = point2;
				this.g.DrawLine(this.blackPen, this.Feeder2_Start, this.Feeder2_End);
			}
		}

		private void CSystemGraphSetDlg_Paint(object sender, PaintEventArgs e)
		{
			this.ShowPanel();
			switch (this.i)
			{
			case 1:
				this.g.DrawLine(new Pen(Color.Red, 2f), this.inLine, this.inLine_End);
				return;
			case 2:
				this.g.DrawLine(new Pen(Color.Red, 2f), this.p_InLink_Start, this.p_InLink_End);
				return;
			case 3:
				this.g.DrawLine(new Pen(Color.Red, 2f), this.Feeder1_Start, this.Feeder1_End);
				this.g.DrawLine(new Pen(Color.Red, 2f), this.Feeder2_Start, this.Feeder2_End);
				return;
			case 4:
				this.g.DrawLine(new Pen(Color.Red, 2f), this.inLink, this.inLink_End);
				this.g.DrawLine(new Pen(Color.Red, 2f), this.inLink1, this.inLink1_End);
				return;
			case 5:
				this.g.DrawLine(new Pen(Color.Red, 2f), this.bus_Width, this.bus_Width_End);
				return;
			default:
				return;
			}
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			if (!this.CheckValue(this.txtnInline, "进线长度"))
			{
				return;
			}
			if (!this.CheckValue(this.txtnlnLink, "接线长度"))
			{
				return;
			}
			if (!this.CheckValue(this.txtnOutLine, "馈线长度"))
			{
				return;
			}
			if (!this.CheckValue(this.txtnOutLink, "接线长度(回路)"))
			{
				return;
			}
			if (!this.CheckValue(this.txtnLoopSpace, "回路间距"))
			{
				return;
			}
			if (!this.CheckValue(this.txtdTextHeight, "标注字高"))
			{
				return;
			}
			this.SaveXMLData();
			base.DialogResult = DialogResult.OK;
		}

		private bool CheckValue(TextBox txt, string msg)
		{
			double num = 0.0;
			if (string.IsNullOrEmpty(txt.Text))
			{
				MessageBox.Show(msg + "不能为空。", "盈建科提示");
				txt.Focus();
				return false;
			}
			if (!double.TryParse(txt.Text, out num))
			{
				MessageBox.Show(msg + "输入的不合法，请重新输入。", "盈建科提示");
				txt.Focus();
				return false;
			}
			return true;
		}

		private void txtnInline_KeyPress(object sender, KeyPressEventArgs e)
		{
			this.CheckInputType(e, this.txtnInline.Text);
		}

		private void txtnlnLink_KeyPress(object sender, KeyPressEventArgs e)
		{
			this.CheckInputType(e, this.txtnlnLink.Text);
		}

		private void txtnOutLine_KeyPress(object sender, KeyPressEventArgs e)
		{
			this.CheckInputType(e, this.txtnOutLine.Text);
		}

		private void txtnOutLink_KeyPress(object sender, KeyPressEventArgs e)
		{
			this.CheckInputType(e, this.txtnOutLink.Text);
		}

		private void txtnLoopSpace_KeyPress(object sender, KeyPressEventArgs e)
		{
			this.CheckInputType(e, this.txtnLoopSpace.Text);
		}

		private void CheckInputInt(KeyPressEventArgs e)
		{
			if (e.KeyChar != '\b' && (e.KeyChar < '0' || e.KeyChar > '9'))
			{
				e.Handled = true;
			}
		}

		private void txtdTextHeight_KeyPress(object sender, KeyPressEventArgs e)
		{
			this.CheckInputType(e, this.txtdTextHeight.Text);
		}

		private void CheckInputType(KeyPressEventArgs e, string txt)
		{
			if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b' && e.KeyChar != '\r' && e.KeyChar != '.')
			{
				e.Handled = true;
			}
			if (e.KeyChar == '.' && txt.IndexOf(".") >= 0)
			{
				e.Handled = true;
			}
		}

		private Graphics g;

		private Pen blackPen;

		private Point inLine;

		private Point inLine_End;

		private Point inLink;

		private Point inLink1;

		private Point inLink_End;

		private Point inLink1_End;

		private Point bus_Width;

		private Point bus_Width_End;

		private Point p_InLink_Start;

		private Point p_InLink_End;

		private Point Feeder1_Start;

		private Point Feeder1_End;

		private Point Feeder2_Start;

		private Point Feeder2_End;

		private int count = 1;

		private int i;

		private YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper();

		private string xmlFile = Path.Combine(Product.WorkingLocation, "SystemGraphSet.xml");
	}
}
